package com.fingard.dsp.bank.directbank.jstzrcb01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * @author dicke
 * @date 2018/9/16 13:44
 */
public class JSTZRCB01SinglePay extends  JSTZRCB01Base implements ISinglePay{
    @Override
    /** 单笔支付*/
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        detail.reqSeqID = p_req1908.singleDetail.askNumber;


        XmlTextWriter xmlWriter =  new XmlTextWriter();
        JSTZRCB01Header header =  new JSTZRCB01Header();
        header.transTime = DateHelper.convertToDateExtra(p_req1908.reqHeader.transTime);
        String opName = "B2EBatchTransfer";
        String TransferType ;
        if(notSameBank.equals(p_req1908.singleDetail.sameBnk)){
            TransferType = "002";
        } else {
            TransferType = "001";
        }
        header.transCode = opName;
        header.reqSeqNo = p_req1908.singleDetail.askNumber;
        xmlWriter.autoFormat = false;
        writeCommonHead(xmlWriter,header);
        xmlWriter.writeStartElement("Body");
        xmlWriter.writeStartElement("List");
        XElement tmpElement  = new XElement("Map","");

        tmpElement.addChild(new XElement("TransferType",TransferType));
        tmpElement.addChild(new XElement("PayerAcNo",p_req1908.singleDetail.payAct));
        tmpElement.addChild(new XElement("PayerAcName",p_req1908.singleDetail.payName));
        //付方账户类型取默认
        tmpElement.addChild(new XElement("PayerBankAcType",this.getBankConfig().getMapValueByKey("cardType", "00")));
        String currency = convertCurToBank(p_req1908.singleDetail.payCur);
        tmpElement.addChild(new XElement("PayerCurrency",currency));
        tmpElement.addChild(new XElement("PayeeAcNo",p_req1908.singleDetail.recAct));
        tmpElement.addChild(new XElement("PayeeAcName",p_req1908.singleDetail.recName));
        tmpElement.addChild(new XElement("PayeeCurrency",currency));
        String cardType = p_req1908.singleDetail.cardType;
        if(StringHelper.isNullOrEmpty(cardType)) {
            cardType = "00";
        }
        cardType = this.getBankConfig().getMapValueByKey("cardType", cardType);
        tmpElement.addChild(new XElement("PayeeBankAcType",cardType));
        tmpElement.addChild(new XElement("UnionDeptId","1"));
        tmpElement.addChild(new XElement("Amount",p_req1908.singleDetail.payAmount));
        tmpElement.addChild(new XElement("Remark",p_req1908.singleDetail.abstractATS));
        nodeAnalysis(p_req1908.singleDetail.srcXElement,tmpElement,getCharset());
        xmlWriter.writeXElement(tmpElement);
        xmlWriter.writeEndDocument();

        String[] retStrArr = sendToBank(xmlWriter.toXmlString(),opName);

        if(retStrArr[0].length() == 0){
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);

            if(tmpBkReader != null){
                String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                String tmpRetInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                if(BANK_RET_RIGHT_CODE.equals(tmpRetCode)){
                    detail.respCode = "0";
                    detail.respInfo = tmpRetInfo;
                    detail.payInfoCode = tmpRetCode;
                    detail.transState = tmpRetCode;
                    detail.transAmt = p_req1908.singleDetail.payAmount;
                    retAts.writeRetDetail(detail);
                }
                else{
                    retAts.writeStart();
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpRetInfo);
                }
            }
            else{
                retAts.writeStart();
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpSbMsg.toString());
            }
        }
        else{
            retAts.writeStart();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(retStrArr[1].toString());
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    /**
     * 支付状态查询
     * */
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();

        String opName = "B2EBatchTrsResQry";
        XmlTextWriter xmlWriter =  new XmlTextWriter();
        JSTZRCB01Header header =  new JSTZRCB01Header();
        header.transCode = opName;
        header.reqSeqNo = p_req1944.askNumber;
        xmlWriter.autoFormat = false;
        writeCommonHead(xmlWriter,header);

        String bgnDate = p_req1944.bgnDate;
        Date date = DateHelper.convertToDateExtra(bgnDate);
        XElement tmpElement  = new XElement("Body","");
        tmpElement.addChild(new XElement("HistoryERPJnlNo",Format.Date8Format.format(date)+header.reqSeqNo));

        xmlWriter.writeXElement(tmpElement);
        xmlWriter.writeEndDocument();

        String[] retStrArr = sendToBank(xmlWriter.toXmlString(),opName);
        if(retStrArr[0].length() == 0){
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);

            if(tmpBkReader != null){
                String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                String tmpRetInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                if(BANK_RET_RIGHT_CODE.equals(tmpRetCode)){
                    detail.respCode = "0";
                    detail.respInfo = tmpRetInfo;
                    detail.payInfoCode = tmpRetCode +"+"+ tmpBkReader.getFirstText("/Message/Body/List/Map/ReturnCode");
                    detail.reqSeqID = p_req1944.reqHeader.reqSeqID;
                    detail.transState = tmpRetCode +"+"+ tmpBkReader.getFirstText("/Message/Body/List/Map/ReturnCode");
                    detail.transAmt = tmpBkReader.getFirstText("/Message/Body/List/Map/Amount");
                    retAts.writeRetDetail(detail);
                }
                else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpRetInfo;
                    retAts.writeRetDetail(detail);
                }
            }
            else{
                detail.respCode = "-1";
                detail.respInfo = tmpSbMsg.toString();
                retAts.writeRetDetail(detail);
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = retStrArr[1].toString();
            retAts.writeRetDetail(detail);
        }
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
